/**
 * 
 */
package com.anduinslegacy.bath;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.anduinslegacy.datastore.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * @author Stephen Jennings
 * 
 */
public class ReminderFactory {
	public static String getReminder() {

		// Create a persistence manager object.
		PersistenceManager pm = PMF.get().getPersistenceManager();

		// Create a transaction.
		Transaction tx = pm.currentTransaction();

		try {
			// Start the transaction.
			tx.begin();

			// Create the key for the object were looking for.
			Key k = KeyFactory.createKey("Reminder", "reminder");

			// Define a variable to hold the returned or create object.
			Reminder reminder;

			// Create a Calendar object.
			Calendar calendar = Calendar.getInstance();
			// Create a Date that represents today.
			Date now = calendar.getTime();

			// Try to get the reminder object from the data store.
			try {
				reminder = pm.getObjectById(Reminder.class, k);
			} catch (JDOObjectNotFoundException e) {
				// getting here means there is no instance of reminder in the
				// data store.
				// so.... make one.
				reminder = new Reminder();
				// Set the default image.
				reminder.setWho("<img src='sebastian.jpg' />");
				// Set the reminders key value.
				reminder.setKey(k);
				// Set the date.
				reminder.setLastUpdate(now);
			}
			// Get the stored or newly set image value.
			String who = reminder.getWho();
			// Get the stored or newly set Date.
			Date lastUpdate = reminder.getLastUpdate();

			// Only do the swap if the day has changed.
			if (isToday(lastUpdate)) {
				// do nothing
			} else {
				reminder.swap();
				reminder.setLastUpdate(now);
			}
			pm.makePersistent(reminder);
			tx.commit();
			return who;
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public static void swap() {
		// Create a persistence manager object.
		PersistenceManager pm = PMF.get().getPersistenceManager();

		// Create a transaction.
		Transaction tx = pm.currentTransaction();

		try {
			// Start the transaction.
			tx.begin();

			// Create the key for the object were looking for.
			Key k = KeyFactory.createKey("Reminder", "reminder");

			// Define a variable to hold the returned or create object.
			Reminder reminder;

			// Create a Calendar object.
			Calendar calendar = Calendar.getInstance();
			// Create a Date that represents today.
			Date now = calendar.getTime();

			// Try to get the reminder object from the data store.
			try {
				reminder = pm.getObjectById(Reminder.class, k);
			} catch (JDOObjectNotFoundException e) {
				// getting here means there is no instance of reminder in the
				// data store.
				// so.... make one.
				reminder = new Reminder();
				// Set the default image.
				reminder.setWho("<img src='images/sebastian.jpg' />");
				// Set the reminders key value.
				reminder.setKey(k);
				// Set the date.
				reminder.setLastUpdate(now);
			}
			reminder.swap();
			reminder.setLastUpdate(now);
			pm.makePersistent(reminder);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	private static boolean isToday(Date date) {
		/* The date passed in here is pulled from the reminder in the data store where it is  
		 * kept in UTC format so we will convert it to local time here. 
		 */
		GregorianCalendar today = new GregorianCalendar(Locale.US); 
		TimeZone tz = TimeZone.getTimeZone("America/Chicago"); 
		today.setTimeZone(tz); 
		today.setTime(new Date());

		GregorianCalendar lastUpdate = new GregorianCalendar(Locale.US);		 
		lastUpdate.setTimeZone(tz); 
		lastUpdate.setTime(date);

		return lastUpdate.get(Calendar.YEAR) == today.get(Calendar.YEAR)
				&& lastUpdate.get(Calendar.MONTH) == today.get(Calendar.MONTH)
				&& lastUpdate.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH);
	}

}
